﻿@namespace iPanel.Components
@using System.Globalization
@using iPanel.Resources
@using iPanel.Utils
@using Aspire.Hosting.ConsoleLogs

@inject IJSRuntime JS
@inject IStringLocalizer<ConsoleLogs> Loc

@implements IAsyncDisposable

<div class="log-overflow console-overflow continuous-scroll-overflow">
    <div class="log-container console-container" id="logContainer">
        @if (LogEntries is { } logEntries)
        {
            <Virtualize Items="@logEntries.GetEntries()" ItemSize="20" OverscanCount="200" TItem="LogEntry">
                @if (context.Pause is { } pause)
                {
                    // If this is a previous pause but no logs were obtained during the pause, we don't need to show anything.
                    if (pause is { FilteredCount: 0, EndTime: not null })
                    {
                        return;
                    }

                    var text = pause.EndTime is null
                        ? string.Format(
                            CultureInfo.CurrentCulture,
                            Loc[nameof(ConsoleLogs.ConsoleLogsPauseActive)],
                            FormatHelpers.FormatTimeWithOptionalDate(TimeProvider, pause.StartTime, MillisecondsDisplay.Truncated),
                            pause.FilteredCount)
                        : string.Format(
                            CultureInfo.CurrentCulture,
                            Loc[nameof(ConsoleLogs.ConsoleLogsPauseDetails)],
                            FormatHelpers.FormatTimeWithOptionalDate(TimeProvider, pause.StartTime, MillisecondsDisplay.Truncated),
                            FormatHelpers.FormatTimeWithOptionalDate(TimeProvider, pause.EndTime.Value, MillisecondsDisplay.Truncated),
                            pause.FilteredCount);

                    <div class="log-line-row-container">
                        <div class="log-line-row console-line-row">
                            <div class="log-line-area" role="log">
                                <span class="log-line-number"></span>
                                <span class="log-content log-pause">@text</span>
                            </div>
                        </div>
                    </div>
                }
                else
                {
                     <div class="log-line-row-container">
                        <div class="log-line-row console-line-row">
                            <span class="log-line-area" role="log">
                                @* <span class="log-line-number">@context.LineNumber</span> *@
                                <span class="log-content">
                                    @{
                                        var hasPrefix = false;
                                    }
                                    @if (ShowTimestamp && context.Timestamp is { } timestamp)
                                    {
                                        hasPrefix = true;
                                        <span class="timestamp" title="@FormatHelpers.FormatDateTime(TimeProvider, timestamp, MillisecondsDisplay.Full, CultureInfo.CurrentCulture)">@GetDisplayTimestamp(timestamp)</span>
                                    }
                                    @if (context.Type == LogEntryType.Error)
                                    {
                                        hasPrefix = true;
                                        <fluent-badge appearance="accent">stderr</fluent-badge>
                                    }
                                    @((MarkupString)((hasPrefix ? "&#32;" : string.Empty) + (context.Content ?? string.Empty)))
                                </span>
                            </span>
                        </div>
                    </div>
                }
            </Virtualize>
        }
    </div>
</div>
